# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from single import *
import rect

fig, ax = plt.subplots()
def Update(iframe):
    pass
ani = FuncAnimation(fig, Update)
ax.set_xlim(0, WIDTH)
ax.set_ylim(0, HEIGHT)
ax.set_aspect(1)

ix = 0
pos_lst = [None] * 2

ln, = plt.plot([], [], "g-")
elpln, = plt.plot([], [], "y-")
pt, = plt.plot([], [], "ro")

def onclick(event):
    global ix, pos_lst
    print(event.xdata, event.ydata)
    i1 = ix
    ix = (ix + 1) % 2
    pos_lst[i1] = [event.xdata, event.ydata]
    if i1 == 1:
        p = GetPath(pos_lst[0], pos_lst[1])
        # elp, sample = GetTarget(pos_lst[0], pos_lst[1])
        elp, sample = rect.GetTarget(pos_lst[0], pos_lst[1])
        ln.set_data(p[:, 0], p[:, 1])
        elpx, elpy = sample
        elpln.set_data(elpx, elpy)
        target, _ = GetTargetSubjectPoint(pos_lst[0], pos_lst[1])
        pt.set_data([target[0]], [target[1]])
cid = fig.canvas.mpl_connect('button_press_event', onclick)

plt.triplot(points[:,0], points[:,1], freetri, "b-")
plt.scatter(obstacle_points.data[:, 0], obstacle_points.data[:, 1])

plt.show()